#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 1 \
    --set-string "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set-string "cluster.pods.scheduling.tolerations[0].effect=NoExecute" \
    --set-string "cluster.pods.scheduling.tolerations[0].key=$NODE_LABEL_KEY" \
    --set-string "cluster.pods.scheduling.tolerations[0].operator=Equal" \
    --set-string "cluster.pods.scheduling.tolerations[0].value=$NODE_LABEL_VALUE" \
    --set "cluster.pods.scheduling.tolerations[0].tolerationSeconds=1" \
    --set-string cluster.distributedLogs.sgDistributedLogs=distributedlogs \
    --set distributedLogs.enabled=true \
    --set-string "distributedLogs.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set-string "distributedLogs.scheduling.tolerations[0].effect=NoExecute" \
    --set-string "distributedLogs.scheduling.tolerations[0].key=$NODE_LABEL_KEY" \
    --set-string "distributedLogs.scheduling.tolerations[0].operator=Equal" \
    --set-string "distributedLogs.scheduling.tolerations[0].value=$NODE_LABEL_VALUE" \
    --set "distributedLogs.scheduling.tolerations[0].tolerationSeconds=1"

}

e2e_test_uninstall() {
  helm_cleanup_chart "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"

  k8s_async_cleanup_namespace "$CLUSTER_NAMESPACE"
}

e2e_test() {
  run_test "sgcluster has specified tolerations" check_sgcluster_has_node_tolerations

  run_test "sgcluster's StatefulSet has specified tolerations" check_sgcluster_sts_has_node_tolerations

  run_test "sgdistributedlogs has specified tolerations" check_sgdistributedlogs_has_node_tolerations

  run_test "sgdistributedlogs's StatefulSet has specified tolerations" check_sgdistributedlogs_sts_has_node_tolerations
}

check_sgcluster_has_node_tolerations() {
  if [ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.pods.scheduling.tolerations 0).effect }}")" \
    = "NoExecute" ]
  then
    success "sgcluster has the configured toleration effect"
  else
    fail "sgcluster has not the configured toleration effect"
  fi

  if [ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.pods.scheduling.tolerations 0).key }}")" \
    = "$NODE_LABEL_KEY" ]
  then
    success "sgcluster has the configured toleration key"
  else
    fail "sgcluster has not the configured toleration key"
  fi

  if [ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.pods.scheduling.tolerations 0).operator }}")" \
    = "Equal" ]
  then
    success "sgcluster has the configured toleration operator"
  else
    fail "sgcluster has not the configured toleration operator"
  fi

  if [ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.pods.scheduling.tolerations 0).value }}")" \
    = "$NODE_LABEL_VALUE" ]
  then
    success "sgcluster has the configured toleration value"
  else
    fail "sgcluster has not the configured toleration value"
  fi

  if [ "$(kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.pods.scheduling.tolerations 0).tolerationSeconds }}")" \
    = "1" ]
  then
    success "sgcluster has the configured toleration tolerationSeconds"
  else
    fail "sgcluster has not the configured toleration tolerationSeconds"
  fi
}

check_sgcluster_sts_has_node_tolerations() {
  echo "Waiting for sts with tolerations"
  try_function wait_until eval "$(cat << 'EOF'
[ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
  --template "{{ (index .spec.template.spec.tolerations 0).effect }}")" \
  = "NoExecute" ]
EOF
    )"
  if "$RESULT"
  then
    success "sgcluster's StatefulSet has the configured toleration effect"
  else
    fail "sgcluster's StatefulSet has not the configured toleration effect"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.template.spec.tolerations 0).key }}")" \
    = "$NODE_LABEL_KEY" ]
  then
    success "sgcluster's StatefulSet has the configured toleration key"
  else
    fail "sgcluster's StatefulSet has not the configured toleration key"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.template.spec.tolerations 0).operator }}")" \
    = "Equal" ]
  then
    success "sgcluster's StatefulSet has the configured toleration operator"
  else
    fail "sgcluster's StatefulSet has not the configured toleration operator"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.template.spec.tolerations 0).value }}")" \
    = "$NODE_LABEL_VALUE" ]
  then
    success "sgcluster's StatefulSet has the configured toleration value"
  else
    fail "sgcluster's StatefulSet has not the configured toleration value"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" \
    --template "{{ (index .spec.template.spec.tolerations 0).tolerationSeconds }}")" \
    = "1" ]
  then
    success "sgcluster's StatefulSet has the configured toleration tolerationSeconds"
  else
    fail "sgcluster's StatefulSet has not the configured toleration tolerationSeconds"
  fi
}

check_sgdistributedlogs_has_node_tolerations() {
  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.scheduling.tolerations 0).effect }}")" \
    = "NoExecute" ]
  then
    success "sgdistributedlogs has the configured toleration effect"
  else
    fail "sgdistributedlogs has not the configured toleration effect"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.scheduling.tolerations 0).key }}")" \
    = "$NODE_LABEL_KEY" ]
  then
    success "sgdistributedlogs has the configured toleration key"
  else
    fail "sgdistributedlogs has not the configured toleration key"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.scheduling.tolerations 0).operator }}")" \
    = "Equal" ]
  then
    success "sgdistributedlogs has the configured toleration operator"
  else
    fail "sgdistributedlogs has not the configured toleration operator"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.scheduling.tolerations 0).value }}")" \
    = "$NODE_LABEL_VALUE" ]
  then
    success "sgdistributedlogs has the configured toleration value"
  else
    fail "sgdistributedlogs has not the configured toleration value"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.scheduling.tolerations 0).tolerationSeconds }}")" \
    = "1" ]
  then
    success "sgdistributedlogs has the configured toleration tolerationSeconds"
  else
    fail "sgdistributedlogs has not the configured toleration tolerationSeconds"
  fi
}

check_sgdistributedlogs_sts_has_node_tolerations() {
  echo "Waiting for sts with tolerations"
  try_function wait_until eval "$(cat << 'EOF'
[ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "distributedlogs" \
  --template "{{ (index .spec.template.spec.tolerations 0).effect }}")" \
  = "NoExecute" ]
EOF
    )"
  if "$RESULT"
  then
    success "sgdistributedlogs's StatefulSet has the configured toleration effect"
  else
    fail "sgdistributedlogs's StatefulSet has not the configured toleration effect"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.template.spec.tolerations 0).key }}")" \
    = "$NODE_LABEL_KEY" ]
  then
    success "sgdistributedlogs's StatefulSet has the configured toleration key"
  else
    fail "sgdistributedlogs's StatefulSet has not the configured toleration key"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.template.spec.tolerations 0).operator }}")" \
    = "Equal" ]
  then
    success "sgdistributedlogs's StatefulSet has the configured toleration operator"
  else
    fail "sgdistributedlogs's StatefulSet has not the configured toleration operator"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.template.spec.tolerations 0).value }}")" \
    = "$NODE_LABEL_VALUE" ]
  then
    success "sgdistributedlogs's StatefulSet has the configured toleration value"
  else
    fail "sgdistributedlogs's StatefulSet has not the configured toleration value"
  fi

  if [ "$(kubectl get sts -n "$CLUSTER_NAMESPACE" "distributedlogs" \
    --template "{{ (index .spec.template.spec.tolerations 0).tolerationSeconds }}")" \
    = "1" ]
  then
    success "sgdistributedlogs's StatefulSet has the configured toleration tolerationSeconds"
  else
    fail "sgdistributedlogs's StatefulSet has not the configured toleration tolerationSeconds"
  fi
}
